package com.practice.stack;

import java.util.ArrayDeque;
import java.util.Deque;

public class SimplifyPath {
	public String simplify(String path) {
		String[] segs = path.split("/");
		Deque<String> d = new ArrayDeque<String>();
		
		for (String seg : segs) {
			if (".".equals(seg) || "".equals(seg)) {
				continue;
			}
			else if ("..".equals(seg)) {
				if (!d.isEmpty()) d.removeLast();
			}
			else { 
				d.addLast(seg);
			}
		}
		
		StringBuilder builder = new StringBuilder();
		while (!d.isEmpty()) {
			builder.append("/" + d.pollFirst());
		}
		
		return builder.toString().length()==0? "/" : builder.toString();
	}
}
